<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

/*
    Function : e107shop_postage_list_regions
    -----------------------------------------------
    Parameters : None
    ===============================================
    This function displays a box with a list
    of current products. A category ID can be sent
    to it to retrieve only items for that category.
    ===============================================
*/

function e107shop_postage_list_regions($product) {
    global $e107shop_db_name, $sql, $ns;
    $query = "";
    
    if(!$sql->db_Select($e107shop_db_name['postage'], "*", $query)) {
        $ns->tablerender("<center>".lan_admin_menu_post."</center>", 
        "<br /><center>".lan_admin_post_regions_empty."</center><br />");
        return;
    } else {    
            
            $display="<center><form name='form1'><br />
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_postage_regions_title."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_postage_regions.": <select class=\"tbox\" name='p' id='p'>";
                                
            while($row=$sql->db_Fetch()){
                    $display.="<option value='".$row["region_id"]."'>".$row["postage_region"]."</option>";
            }
                
            
            $display.="
            </select>&nbsp;&nbsp;";

            $display.="<input class=\"tbox\" type='button' name='a' value='"
            .lan_global_edit."' OnClick=\"window.location='admin_manage_regions.php?a=1&p='
            +document.form1.p.options[document.form1.p.selectedIndex].value\">&nbsp;&nbsp;";
            $display.="<input class=\"tbox\" type='button' name='d' value='"
            .lan_global_delete."' OnClick=\"window.location='admin_manage_regions.php?a=2&p='
            +document.form1.p.options[document.form1.p.selectedIndex].value\">";
            $display.="</form>";
            $display.="</td></tr></table>";
        }
    
    $ns->tablerender("<center>".lan_admin_menu_post."</center>", $display);
    
}

/*
    Function : e107shop_manage_postage
    --------------------------------------------------
    Parameters :    $action     The action to take
                    $region     The region
    ==================================================
    This function displays the form for adding/editing
    a product.
    ==================================================
*/

function e107shop_manage_postage($action, $region) {
    global $e107shop_db_name, $sql, $ns;
    if($action==1) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['postage'], "*", "region_id=".$region);
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $region_id = $row["region_id"];
            $postage_region=$row["postage_region"];
            $postage_price=$row["postage_price"];
            $postage_extra = $row["extra"];    
            
        }
        $formaction = 1;
    } else if($action==2) {
        $sql->db_Delete($e107shop_db_name['postage'], "region_id=".$region);
        
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center><br />".lan_global_deleted."</center>");
        $formaction = 2;
    } else {
        $formaction = 2;
    }

    //
    // Display form
    //

    $display = "<center><form action='".e_SELF."' method='POST' name='productform' ID='productform'>
                <table width='95%' class='fborder'><br />
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_manage_postage_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='region_name' value='".$postage_region."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_postage_price.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='postage_price' value='".$postage_price."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prostage_extra.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='postage_extra' value='".$postage_extra."'>
                        </td>
                    </tr>            
                    <tr>
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='".$formaction."' name='faction'>
                            <input class=\"tbox\" type='hidden' value='".$region."' name='region'>
                            <center>";
    if ($action == 1) {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_update."' name='subbed'>";
    } else {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_submit."' name='subbed'>";
    }
    
    $display .= "</center></td></tr></form></table>";

    if($action==1) {
        $ns->tablerender("<center>".lan_admin_post_regions_edit."</center>", $display);
    } else {
        $ns->tablerender("<center>".lan_admin_post_regions_new."</center>", $display);
    }
}

/*
    Function : e107shop_process_attributes
    --------------------------------------------
    Parameters :    $action     The action to take
    ==============================================
    This function processes the product and adds
    delets or updates it in the database.
    ==============================================
*/

function e107shop_process_postage_regions($action, $region_name, $region_price, $postage_extra, $region) {
    
    global $e107shop_db_name, $sql, $ns;
    if($action==1) {
        //
        // edit
        //
        $sql->db_Update($e107shop_db_name['postage'], "postage_region='".$region_name."', postage_price='".$region_price."', extra='".$postage_extra."' where region_id=".$region);
        $ns->tablerender("<center>".lan_global_status."</center>", "<center>".lan_global_updated."</center>");
    } else {
        //
        // Insert a new product
        //
        if ($region_name != '') $sql->db_Insert($e107shop_db_name['postage'], "0, '".$region_name."', '"
        .$region_price."', '".$postage_extra."'");
    }
}
